package board.spring.mvc.board.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import board.spring.mvc.board.model.Article;
import board.spring.mvc.board.interfaces.ArticleService;

@Controller
@RequestMapping("/board/*")
@SessionAttributes("article")
public class ArticleController {

	@Autowired private ArticleService articleService;
	
	@RequestMapping
	public void list(Model model) {
		model.addAttribute("articles", articleService.selectArticles());
	}
	
	@RequestMapping(value="/view", method=RequestMethod.GET)
	public Article view(int num) {
		return articleService.selectArticle(num);
	}
	
	@RequestMapping(value="/write", method=RequestMethod.GET)
	public void writeFrom() {}
	
	@RequestMapping(value="/write", method=RequestMethod.POST)
	public String write(@ModelAttribute("newArticle")Article article) {
		articleService.insertArticle(article);
		return "redirect:list";
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.GET)
	public String delete(int num) {
		articleService.deleteArticle(num);
		return "redirect:list";
	}
}



























